//index.js
//获取应用实例
var util = require('../../utils/util.js');
const app = getApp()//全局变量
var appInstance = getApp()//全局变量
Page({
  data: {
    code: '',
    openid: '',
    in: '',
    out: '',
    budget: appInstance.globalData.budget,
    date: '2016-09-01',
    time: '12:01',
    year: 2000,
  },
  //跳转到收入支出界面
  goToChargePage: function () {
    wx.navigateTo({
      url: '../jj/jj',
    })
  },

  //跳转至报表页面函数
  goToProgramPage: function () {
    wx.navigateTo({
      url: '../program/program',
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    // 调用函数时，传入new Date()参数，返回值是日期和时间  
    var time = util.formatTime(new Date());
    var dotp = time.indexOf("/");
    var year = time.substr(0, 4);
    var month = time.substr(dotp + 1, 2);
    // 再通过setData更改Page()里面的data，动态更新页面的数据  
    this.time = time;
    this.year = parseInt(year);
    this.month = parseInt(month);
  },
  onShow: function () {
    var t1 = this;
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    
    wx.login({
      //获取code
      success: function (res) {
        var t2 = t1;
        this.code = res.code; //返回code

        wx.request({
          url: 'https://hi.slickghost.com/WebTest/InsertUsers',
          header: {
            'content-type': 'json'
          },
          data: {
            code: res.code
          },
          success: function (res) {
            var t3 = t2;
            app.globalData.openid = res.data;
            wx.request({
              url: 'https://hi.slickghost.com/WebTest/Operate',
              data: {
                choice: 'insertopenid',
                openid: app.globalData.openid
              },
            })
            wx.request({
              url: 'https://hi.slickghost.com/WebTest/Operate',
              data: {
                choice: 'findbudget',
                openid: appInstance.globalData.openid,
                year: appInstance.data.year,
                month: appInstance.data.month
              },

              header: {
                'content-type': 'application/json' // 默认值
              },
              
              //数据库返回值赋值
              success: (res) => {
                var that = t3;
                that.setData({
                  budget: res.data
                });
              }
            }),
        
            //数据库查找收入支出
            wx.request({
              url: 'https://hi.slickghost.com/WebTest/Operate',
              data: {
                choice: 'findin_out',
                openid: app.globalData.openid,
                year: t3.year,
                month: t3.month
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              //收入支出提取并设置为全局变量
              success: (res) => {
                var t4 = t3;
                var in_out = JSON.stringify(res.data);
                var str = in_out.substr(1, in_out.length - 2);
                var dotp1 = in_out.indexOf(",");
                var dotp2 = in_out.indexOf(",", dotp1 + 1);
                app.globalData.out = str.substr(0, dotp1 - 1);
                app.globalData.in = str.substr(dotp1, dotp2 - dotp1 - 1);
                t4.setData({
                  in: app.globalData.in,
                  out: app.globalData.out
                });

              }
            })
          }

        })
      }
    })
    /*数据库查找月预算*/

  },
  //获取用户信息
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
